import { UNI_STORAGE_KEY_TOKEN } from "../utils/constants";

/**
 * 与后端约定鉴权请求头
 */
export const AUTH_HEADER_KEY = "Authorization";
export const TOKEN_HEAD = "Bearer ";

/**
 * Http请求工具类，封装wx.request
 */
export class HttpClient {
    baseUrl = 'http://localhost:8081';

    getRequest() {
        return wx.request;
    }

    get(url, needAuth, config,) {
        url = this.wrapperUrl(url);
        return new Promise((resole, reject) => {
            wx.request({
                url: url,
                method: "GET",
                dataType: config && config.dataType || "json",
                header: this.appendAuthHeader(needAuth, config),
                success: function (res) {
                    if (res.statusCode != 200) {
                        reject(res);
                    } else {
                        resole(res);
                    }
                },
                fail: function (err) {
                    reject(err);
                }
            });
        });
    }

    post(url, data, needAuth, config,) {
        url = this.wrapperUrl(url);
        return new Promise((resole, reject) => {
            wx.request({
                url: url,
                method: "POST",
                dataType: config && config.dataType || "json",
                data: data,
                header: this.appendAuthHeader(needAuth, config),
                success: function (res) {
                    if (res.statusCode != 200) {
                        reject(res);
                    } else {
                        resole(res);
                    }
                },
                fail: function (err) {
                    reject(err);
                }
            });
        });
    }

    put(url, data, needAuth, config,) {
        url = this.wrapperUrl(url);
        return new Promise((resole, reject) => {
            wx.request({
                url: url,
                method: "PUT",
                dataType: config && config.dataType || "json",
                data: data,
                header: this.appendAuthHeader(needAuth, config),
                success: function (res) {
                    if (res.statusCode != 200) {
                        reject(res);
                    } else {
                        resole(res);
                    }
                },
                fail: function (err) {
                    reject(err);
                }
            });
        });
    }

    patch(url, data, needAuth, config,) {
        url = this.wrapperUrl(url);
        return new Promise((resole, reject) => {
            wx.request({
                url: url,
                method: "PATCH",
                dataType: config && config.dataType || "json",
                data: data,
                header: this.appendAuthHeader(needAuth, config),
                success: function (res) {
                    if (res.statusCode != 200) {
                        reject(res);
                    } else {
                        resole(res);
                    }
                },
                fail: function (err) {
                    reject(err);
                }
            });
        });
    }

    delete(url, needAuth, config,) {
        url = this.wrapperUrl(url);
        return new Promise((resole, reject) => {
            wx.request({
                url: url,
                method: "DELETE",
                dataType: config && config.dataType || "json",
                header: this.appendAuthHeader(needAuth, config),
                success: function (res) {
                    if (res.statusCode != 200) {
                        reject(res);
                    } else {
                        resole(res);
                    }
                },
                fail: function (err) {
                    reject(err);
                }
            });
        });
    }

    request(url, data, needAuth, config) {
        url = this.wrapperUrl(url);
        return new Promise((resole, reject) => {
            wx.request({
                url: url,
                method: config && config.method || "GET",
                data: data,
                dataType: config && config.dataType || "json",
                header: this.appendAuthHeader(needAuth, config),
                success: function (res) {
                    if (res.statusCode != 200) {
                        reject(res);
                    } else {
                        resole(res);
                    }
                },
                fail: function (err) {
                    reject(err);
                }
            });
        });
    }

    wrapperUrl(url) {
        return `${this.baseUrl}${url}`;
    }

    /**
     * 添加鉴权请求头
     * @param needAuth 是否需要鉴权，默认为true
     * @param config 
     * @returns 
     */
    appendAuthHeader(needAuth, config,) {
        if (!config) {
            config = {
                headers: {}
            };
        } else if (config && !config.headers) {
            config.headers = {};
        }
        needAuth = needAuth != null ? needAuth : true;
        if (!needAuth) {
            return config && config.headers;
        }
        const token = uni.getStorageSync(UNI_STORAGE_KEY_TOKEN);
        if (config && config.headers && !config.headers[AUTH_HEADER_KEY]) {
            config.headers[AUTH_HEADER_KEY] = TOKEN_HEAD + token;
        }

        return config.headers;
    }
}

export const httpClient = new HttpClient();

export default httpClient;